package com.admin.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyInvocationHandler implements InvocationHandler {
	private Object obj;

	public MyInvocationHandler(Object obj) {
		this.obj = obj;
	}

	public <T> T newProxyInstance() {
		return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if (method.getName().equals("ask")) {
			System.out.println("before");
			String arg = args[0].toString();
			args[0] = "Tom" + arg;
			Object obj = method.invoke(this.obj, args);
			System.out.println("after");
			return obj;
		}
		return method.invoke(this.obj, args);
	}

}
